package com.meida.student.app.user.handler;


import com.alibaba.fastjson.util.TypeUtils;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.ApiAssert;
import com.meida.im.provider.service.RongCloudService;
import com.meida.module.bus.provider.service.BusScholartyrantService;
import com.meida.module.live.io.agora.utils.RtmTokenBuilderUtil;
import com.meida.module.pay.client.entity.PayBalance;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.user.client.entity.AppSchool;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户信息初始化扩展
 */
@Component
public class UserInitHandler implements GetInterceptor {

    @Autowired
    private PayBalanceService payBalanceService;

    @Autowired
    private RongCloudService rongCloudService;

    @Autowired
    private BusScholartyrantService scholartyrantService;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private RtmTokenBuilderUtil rtmTokenBuilderUtil;


    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.select(AppSchool.class, "schoolName", "schoolAbridge");
        cq.createJoin(AppSchool.class);

    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        map.put("isSetPayPwd", false);
        String loginType = cq.getParams("loginType");
        Integer schoolAuth = map.getInt("schoolAuth");
        map.put("schoolAuth", schoolAuth);
        ApiAssert.isNotEmpty("loginType不能为空", loginType);
        Long userId = OpenHelper.getUserId();
        String userType = map.get("userType");
        Boolean openXueBa = false;
        if (userType.equals("SCHOLARTYRANT")) {
            openXueBa = true;
        }
        if (!loginType.equals("xbForm")) {
            map.put("userType", "USER");
        }
        PayBalance payBalance = payBalanceService.getByUserId(userId);
        if (FlymeUtils.isNotEmpty(payBalance)) {
            String paypwd = payBalance.getPaypwd();
            if (FlymeUtils.isNotEmpty(paypwd)) {
                map.put("isSetPayPwd", true);
            }
        }
        Boolean isScholarTyrant = scholartyrantService.isScholarTyrant(userId);
        map.put("isScholarTyrant", isScholarTyrant);
        map.put("openXueBa", openXueBa);
        String userToken = map.get("userToken");
        if (FlymeUtils.isEmpty(userToken)) {
            Long userId1 = map.getLong("userId");
            String nickName = map.get("nickName");
            String avatar = map.get("avatar");
            userToken = rongCloudService.register(TypeUtils.castToString(userId1), nickName, avatar);
            CriteriaUpdate cu = new CriteriaUpdate();
            cu.eq("userId", userId1);
            cu.set("userToken", userToken);
            appUserService.update(cu);
        }
        map.put("userToken", userToken);
        String rtmToken = rtmTokenBuilderUtil.getRtmToken(String.valueOf(userId), 3600 * 24);
        map.put("rtmToken", rtmToken);
    }
}
